class Solution {
    public int shipWithinDays(int[] ws, int d) {
        int l = 1, r = (int)1e9;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(ws, mid, d)) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        return r;
    }
    public boolean check(int[] weights, int mid, int days)
    {

        int fast = 0;
        int flag = 0;
        int count = 1;
        int len = weights.length;
        while (fast < len)
        {
            if (weights[fast] > mid) return false;
            if (flag + weights[fast] <= mid)
                flag += weights[fast];
            else
            {
                count++;
                flag = weights[fast];
            }
            fast++;
        }
        if (count > days)
        {
            return false;
        }
        else return true;
    }
}